package com.da.javatest.thread.other;

public class DeadLockDemo2 {

    private static final Object lockA = new Object();

    private static final Object lockB = new Object();

    public static void main(String[] args) {
        new DeadLockDemo2().deadLock();
    }

    private void deadLock() {

        Thread thread1 = new Thread(this::getLock, "Thread1");

        Thread thread2 = new Thread(this::getLock, "Thread2");

        thread1.start();
        thread2.start();

    }

    private void getLock() {
        synchronized (lockA) {
            try {
                System.out.println(Thread.currentThread().getName() + "获取A锁ing！");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "睡眠1000ms");
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "需要B锁！！！");
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "B锁获取成功");
            }
        }
    }

}